/** Number of rolls */
var count=1;

var user = new String;

function clearRolls()
{
        var div = document.getElementById('rollDiv');
        div.innerHTML = "";      
}

function getMessage()
{
        return document.getElementById("chat").value;
}

function getTimes()
{
        var value=document.getElementById("times").value;
        if(value<1)
                value=1;
        return value;
}

/**
Checks if the button Public rolls is checked
*/
function isPublic()
{
        return document.getElementById("publicrolls").checked;
}

function getNick()
{
        var value=document.getElementById("nick").value;
        return value;
}

function searchSkillValue()
{
    var skill=getSkill();
    if(skill!=="")
    {
       //The skill is referenced to the partipant
       gapi.hangout.data.submitDelta( {'nick':getNick(), 'skill': skill , 'value': getMod()} );
       console.log(skill+" "+getMod());
    }
    
}

/**
Get the skill if any and stores it in state
*/
function getSkill()
{
        var skill=document.getElementById("skill").value;
        return skill;
}

function getMod()
{
        var mod= document.getElementById("modifier").value;
        if(mod>0) mod='+'+mod;
        return mod;
}


function setNick(nick)
{
        var inputNick=document.getElementById("nick");
        inputNick.setAttribute("value", nick);
}

function showParticipants() {
  var participants = gapi.hangout.getParticipants();
  var retVal = '<p>Participants: </p><ul>';
  for (var index in participants) {
    var participant = participants[index];

    if (!participant.person) {
      retVal += '<li>A participant not running this app</li>';
    }
    retVal += '<li>' + participant.person.displayName + '</li>';
  }

  retVal += '</ul>';

  var div = document.getElementById('mdrParticipants');

  div.innerHTML = retVal;
}
         
function selectParticipants() {
  var participants = gapi.hangout.getParticipants();
  var retVal = '<select>';
  for (var index in participants) {
    var participant = participants[index];

    if (!participant.person) {
      retVal += '<option>A participant not running this app</option>';
    }
    retVal += '<option value=\"'+ participant.person.displayName+ '\">'+participant.person.displayName+'</option>';
  }

  retVal += '</select>';

  var div = document.getElementById('mdrParticipants');

  div.innerHTML = retVal;
}
         
        
function roll(sides,times) {
  
  	var index='<p>'+'['+count+'] ';
        var retVal = times+'d'+sides+'= ';
        var message = '<p>['+getNick()+'] ';
        var ammount=0;
        var result= "";

        for (var n=0;n<times;n++)
        {
            var roll=Math.floor(Math.random()*sides)+1;
            ammount = ammount+roll;
            result=result+roll;
            if(times>1 && n<times-1)
                result=result+'+';
        }
        
        var skillroll=ammount+new Number(getMod());
        
         if(getSkill()!="")
        	retVal=getSkill()+'=<strong>'+skillroll+'</strong> '+retVal;
        
        message = message+retVal;
        retVal = index+retVal;
     
        if(times>1)
        {
                retVal += '<strong>'+ammount+'</strong><i>'+' ('+result+')</i></p>';
                message+= '<strong>'+ammount+'</strong><i>'+' ('+result+')</i></p>';
        }
        else{
                retVal += '<strong>'+ammount+'</strong></p>';
                message+='<strong>'+ammount+'</strong></p>';
        }
               
        var div = document.getElementById('rollDiv');
        var content = div.innerHTML;
        div.innerHTML =retVal+content;
        count++;
      
        if(isPublic())
            gapi.hangout.data.sendMessage(message);
}

function rollFudge(times) {

	var index='<p>'+'['+count+'] ';
  		
        var retVal = times+'df= ';
        
       
        var message = '<p class=\"message\">['+getNick()+'] ';
        var ammount=0;
        var result='';
        for (var n=0;n<times;n++)
        {
        var roll = Math.floor(Math.random()*6)+1;
                switch(roll)
                {
                        case 1:
                        case 2: 
                                ammount--;
                                result=result+" -";
                        break;
                        case 5:
                        case 6:
                                ammount++;
                                result=result+" +";
                                break;
                        default:
                                result=result+" x";
                                break;
                }
        }
   
        var skillroll=ammount+new Number(getMod());
        
        if(getSkill()!="")
            retVal=getSkill()+'=<strong>'+skillroll+'</strong> '+retVal;
        
        message = message+retVal;
        retVal = index+retVal;
        
        if(times>1)
        {
                retVal += '<strong>'+ammount+'</strong><i>'+' ('+result+')</i></p>';
                message +='<strong>'+ammount+'</strong><i>'+' ('+result+')</i></p>';
        }
        else
        {
                retVal += '<strong>'+ammount+'</strong></p>';
                message+= '<strong>'+ammount+'</strong></p>';
        }
        
        count++;
        var div = document.getElementById('rollDiv');
        var content = div.innerHTML;
        div.innerHTML = retVal+content;
   		
   		if(isPublic())
    	gapi.hangout.data.sendMessage(message);
 
}

function onMessageReceived(event) {
  try {
    

    
    var div = document.getElementById('rollDiv');
    var content = div.innerHTML;
    div.innerHTML = event.message+content;
    
  } catch (e) {
    console.log(e);
  }
}

function init() {
  // When API is ready...                                                         
  gapi.hangout.onApiReady.add(
      function(eventObj) {
        if (eventObj.isApiReady) {
        
        try {
            gapi.hangout.data.onMessageReceived.add(onMessageReceived);

            document.getElementById('rollD4')
            .style.visibility = 'visible';
            document.getElementById('rollD6')
            .style.visibility = 'visible';
            document.getElementById('rollD8')
            .style.visibility = 'visible';
            document.getElementById('rollD10')
            .style.visibility = 'visible';
            document.getElementById('rollD12')
            .style.visibility = 'visible';
            document.getElementById('rollD20')
            .style.visibility = 'visible';
            document.getElementById('rollD100')
            .style.visibility = 'visible';
            document.getElementById('rollfudge')
            .style.visibility = 'visible';
            
            var participant = gapi.hangout.getLocalParticipant();
            user = participant.person.displayName;
            setNick(user);
            
            //selectParticipants();
           
          } catch (e) {
            console.log('init:ERROR');
            console.log(e);
          }
         
           
        }
      });
}

// Wait for gadget to load.                                                       
gadgets.util.registerOnLoadHandler(init);